package com.jdbc;

import java.sql.SQLOutput;
import java.util.List;
import java.util.Scanner;

public class homework {
    private static User currentUser; // 添加用户对象存储变量

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // 登录系统
        System.out.println("请输入账号:");
        String username = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();

        // 验证用户
        currentUser = JDBCUtils.query(User.class, "select * from pet_user where username = ? and password = ?", username, password).stream().findFirst().orElse(null);
        if (currentUser == null) {
            System.out.println("用户名或密码错误!");
            return;
        }

        // 根据用户类型显示不同的功能菜单
        if (currentUser.isAdmin()) {
            adminMenu(scanner);
        } else {
            customerMenu(scanner);
        }
    }

    private static void adminMenu(Scanner scanner) {
        // 管理员功能菜单
        boolean exit = false;
        while (!exit) {
            System.out.println("管理员功能菜单:");
            System.out.println("1. 添加宠物");
            System.out.println("2. 更新宠物");
            System.out.println("3. 删除宠物");
            System.out.println("4. 查看宠物");
            System.out.println("5. 退出");
            System.out.print("请选择功能: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addPet(scanner);
                    break;
                case 2:
                    updatePet(scanner);
                    break;
                case 3:
                    deletePet(scanner);
                    break;
                case 4:
                    viewPets();
                    break;
                case 5:
                    exit = true;
                    break;
                default:
                    System.out.println("无效的选择!");
            }
        }
    }

    private static void customerMenu(Scanner scanner) {
        // 客户功能菜单
        boolean exit = false;
        while (!exit) {
            System.out.println("客户功能菜单:");
            System.out.println("1. 查看宠物");
            System.out.println("2. 购买宠物");
            System.out.println("3. 退出");
            System.out.print("请选择功能: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    viewPets();
                    break;
                case 2:
                    buyPet(scanner);
                    break;
                case 3:
                    exit = true;
                    break;
                default:
                    System.out.println("无效的选择!");
            }
        }
    }

    private static void addPet(Scanner scanner) {
        System.out.print("请输入宠物名称: ");
        String name = scanner.nextLine();
        System.out.print("请输入宠物主人ID: ");
        int host_id = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符
        System.out.print("请输入宠物年龄: ");
        int age = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符
        System.out.print("请输入宠物性别: ");
        String gender = scanner.nextLine();

        Pet pet = new Pet(host_id, name, age, gender);
        JDBCUtils.update("insert into pet (host_id, name, age, gender) values (?, ?, ?, ?)", pet.getHost_id(), pet.getName(), pet.getAge(), pet.getGender());
        System.out.println("宠物添加成功!");
    }

    private static void updatePet(Scanner scanner) {
        System.out.print("请输入宠物ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符
        System.out.print("请输入新的宠物名称: ");
        String name = scanner.nextLine();
        System.out.print("请输入新的宠物主人ID: ");
        int host_id = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符
        System.out.print("请输入新的宠物年龄: ");
        int age = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符
        System.out.print("请输入新的宠物性别: ");
        String gender = scanner.nextLine();

        Pet pet = new Pet(id, host_id, name, age, gender);
        JDBCUtils.update("update pet set host_id = ?, name = ?, age = ?, gender = ? where id = ?", pet.getHost_id(), pet.getName(), pet.getAge(), pet.getGender(), pet.getId());
        System.out.println("宠物更新成功!");
    }

    private static void deletePet(Scanner scanner) {
        System.out.print("请输入宠物ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        JDBCUtils.update("delete from pet where id = ?", id);
        System.out.println("宠物删除成功!");
    }

    private static void viewPets() {
        if (currentUser.isAdmin()) {
            List<Pet> pets = JDBCUtils.query(Pet.class, "select * from pet ");
            for (Pet pet : pets) {
                System.out.println(pet);
            }
        } else {
            List<Pet> pets = JDBCUtils.query(Pet.class, "select * from pet where host_id = 0");
            for (Pet pet : pets) {
                System.out.println(pet);
            }
        }
    }

    private static void buyPet(Scanner scanner) {
        System.out.print("请输入宠物ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        Pet pet = JDBCUtils.query(Pet.class, "select * from pet where id = ?", id).stream().findFirst().orElse(null);
        if (pet == null) {
            System.out.println("宠物不存在!");
            return;
        }
//        System.out.println("123456");
        JDBCUtils.update("update pet set host_id = ? where id = ?", currentUser.getId(), pet.getId());
        System.out.println("宠物更新成功!");

    }

    private static User getCurrentUser() {
        return currentUser; // 返回存储的当前用户对象
    }
}